{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "乐学偶得版权所有  公众号：乐学Fintech  主讲人：William LX_LAB教研团队提供技术支持 正版视频： 网易云课堂：乐学偶得"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "18.写一个函数，能够计算两个数的GCD(Greatest Common Divisior)最大公约数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "tips:GCD(Greatest Common Divisior)最大公约数\n",
    "\n",
    "10 5\n",
    "1.10/5=2 --5\n",
    "\n",
    "质因数分解法\n",
    "2.24 60\n",
    "24=2*2*2*3\n",
    "60=2*2*3*5\n",
    "公共的质因数：2，2，3\n",
    "把公共的质因数相乘：2*2*3=12\n",
    "（24，60）=12"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculate_GCD(x,y):\n",
    "    gcd = 1\n",
    "    \n",
    "    if x % y ==0:\n",
    "        return y\n",
    "    \n",
    "    for k in range(int(y/2),0,-1):\n",
    "        if x % k ==0 and y % k ==0:\n",
    "            gcd = k\n",
    "            break\n",
    "    return gcd\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n"
     ]
    }
   ],
   "source": [
    "print(calculate_GCD(60,24))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "print(calculate_GCD(10,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "print(calculate_GCD(5,10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
